/* pngx extension for PHP */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "php.h"
#include "ext/standard/info.h"
#include "php_pngx.h"

// PNGX Socket 依赖的库
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

// 监听 IP 和端口
void pngx_socket_listen(const char *addr, int port)
{
    int server_sock;
    struct sockaddr_in server_addr;
    server_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(addr);
    server_addr.sin_port = htons((int)port);
    bind(server_sock, (struct sockaddr*)&server_addr, sizeof(server_addr));
    listen(server_sock, 20);
    struct sockaddr_in clnt_addr;
    socklen_t clnt_addr_size = sizeof(clnt_addr);
    while(1) {
        int clnt_sock = accept(server_sock, (struct sockaddr*)&clnt_addr, &clnt_addr_size);
        pngx_socket_worker(clnt_sock);
    }
}

// Worker 工作进程, 得到和客户端交互的 socket
void pngx_socket_worker(int clnt_sock)
{
    //向客户端发送数据
    char str[] = "http://c.biancheng.net/socket/";
    write(clnt_sock, str, sizeof(str));
    close(clnt_sock);
}

/* For compatibility with older PHP versions */
#ifndef ZEND_PARSE_PARAMETERS_NONE
#define ZEND_PARSE_PARAMETERS_NONE() \
	ZEND_PARSE_PARAMETERS_START(0, 0) \
	ZEND_PARSE_PARAMETERS_END()
#endif

/* {{{ void pngx_test1()
 */
PHP_FUNCTION(pngx_test1)
{
	ZEND_PARSE_PARAMETERS_NONE();

	php_printf("The extension %s is loaded and working!\r\n", "pngx");
}
/* }}} */

PHP_FUNCTION(pngx_start)
{
    ZEND_PARSE_PARAMETERS_NONE();
    pngx_socket_listen("0.0.0.0", 3333);
}

/* {{{ string pngx_test2( [ string $var ] )
 */
PHP_FUNCTION(pngx_test2)
{
	char *var = "World";
	size_t var_len = sizeof("World") - 1;
	zend_string *retval;

	ZEND_PARSE_PARAMETERS_START(0, 1)
		Z_PARAM_OPTIONAL
		Z_PARAM_STRING(var, var_len)
	ZEND_PARSE_PARAMETERS_END();

	retval = strpprintf(0, "Hello %s", var);

	RETURN_STR(retval);
}
/* }}}*/

/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(pngx)
{
#if defined(ZTS) && defined(COMPILE_DL_PNGX)
	ZEND_TSRMLS_CACHE_UPDATE();
#endif

	return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(pngx)
{
	php_info_print_table_start();
	php_info_print_table_header(2, "pngx support", "enabled");
	php_info_print_table_end();
}
/* }}} */

/* {{{ arginfo
 */
ZEND_BEGIN_ARG_INFO(arginfo_pngx_test1, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_pngx_start, 0)
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_INFO(arginfo_pngx_test2, 0)
	ZEND_ARG_INFO(0, str)
ZEND_END_ARG_INFO()
/* }}} */

/* {{{ pngx_functions[]
 */
static const zend_function_entry pngx_functions[] = {
	PHP_FE(pngx_test1,		arginfo_pngx_test1)
	PHP_FE(pngx_start,		arginfo_pngx_start)
	PHP_FE(pngx_test2,		arginfo_pngx_test2)
	PHP_FE_END
};
/* }}} */

/* {{{ pngx_module_entry
 */
zend_module_entry pngx_module_entry = {
	STANDARD_MODULE_HEADER,
	"pngx",					/* Extension name */
	pngx_functions,			/* zend_function_entry */
	NULL,							/* PHP_MINIT - Module initialization */
	NULL,							/* PHP_MSHUTDOWN - Module shutdown */
	PHP_RINIT(pngx),			/* PHP_RINIT - Request initialization */
	NULL,							/* PHP_RSHUTDOWN - Request shutdown */
	PHP_MINFO(pngx),			/* PHP_MINFO - Module info */
	PHP_PNGX_VERSION,		/* Version */
	STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_PNGX
# ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
# endif
ZEND_GET_MODULE(pngx)
#endif

